 ///
 /// @file    threadpool.c
 /// @author  lemon(haohb13@gmail.com)
 /// @date    2022-06-06 11:01:26
 ///
 
#include "threadpool.h"
 
void threadpoolInit(thread_pool_t* pthreadpool, int threadNum)
{
	pthreadpool->threadNumber = threadNum;
	pthreadpool->threads = (pthread_t*)calloc(threadNum, sizeof(pthread_t));
	queueInit(&pthreadpool->queue);
    pthreadpool->epfd=0;
}

void threadpoolDestroy(thread_pool_t* pthreadpool)
{
	free(pthreadpool->threads);
	queueDestroy(&pthreadpool->queue);
}

void threadpoolStart(thread_pool_t * pthreadpool)
{
	for(int idx = 0; idx < pthreadpool->threadNumber; ++idx) {
		int ret = pthread_create(&pthreadpool->threads[idx],
			NULL, 
			threadFunc, 
			pthreadpool);
		THREAD_ERROR_CHECK(ret, "pthread_create");
	}
}

void * threadFunc(void* arg)
{
	//每一个子线程要做的事儿
	printf("sub thread %ld is runing\n", pthread_self());
	thread_pool_t * pthreadpool = (thread_pool_t*)arg;
	while(1) {
		task_t *ptask = taskDequeue(&pthreadpool->queue);
		if(ptask->peerfd > 0) {
            switch(ptask->type)
            {
                
            case CMD_TYPE_CD: 
                {
                    puts("CMD_TYPE_CD");
                    printf("The CMD is %d\n", ptask->type);
                    printf("What received: ");
                    puts(ptask->data);
                    func_cd(ptask->peerfd, ptask->data); 
                    //char filename[] = "The_Holy_Bible.txt";
                    //transferfile(ptask->peerfd, filename);
                    
                    netdiskLog("CMD:CD exec one time.\n");
                    free(ptask);
                    break;
                }
            case CMD_TYPE_RM:
                {
                    puts("CMD_TYPE_RM");
                    printf("The CMD is %d\n", ptask->type);
                    printf("What received: ");
                    puts(ptask->data);
                    func_rm(ptask->peerfd, ptask->data); 
                    
                    netdiskLog("CMD:RM exec one time.\n");
                    free(ptask);
                    break;
                }
            case CMD_TYPE_MKDIR:
                //未处理好权限设置问题
                {
                    puts("CMD_TYPE_MKDIR");
                    printf("The CMD is %d\n", ptask->type);
                    printf("What received: ");
                    puts(ptask->data);
                    func_mkdir(ptask->peerfd,ptask->data); 
                    netdiskLog("CMD:MKDIR exec one time.\n");
                    free(ptask);
                    break;
                }
            case CMD_TYPE_LS:
                {
                    puts("CMD_TYPE_LS");
                    printf("The CMD is %d\n", ptask->type);
                    printf("What received: ");
                    puts(ptask->data);
                    func_ls(ptask->peerfd, ptask->data); 
                    netdiskLog("CMD:LS exec one time.\n");
                    free(ptask);
                    break;
                }
            case CMD_TYPE_PWD:
                {
                    puts("CMD_TYPE_PWD");
                    printf("The CMD is %d\n", ptask->type);
                    printf("What received: ");
                    puts(ptask->data);
                    func_pwd(ptask->peerfd); 
                    netdiskLog("CMD:PWD exec one time.\n");
                    free(ptask);
                    break;
                }
            case CMD_TYPE_GETS:
                
                {
                    puts("CMD_TYPE_GETS");
                    //将peerfd剔除监听队列，被上传文件的客户端独占
                    epollDelEvent(pthreadpool->epfd,ptask->peerfd);
                    func_gets_filename(ptask);
                    netdiskLog("CMD:GETS FILE exec one time.\n");
                    epollAddReadEvent(pthreadpool->epfd,ptask->peerfd);
                    free(ptask);
                    break;
                }
            case CMD_TYPE_PUTS:
                {
                    puts("CMD_TYPE_PUTS");
                    //将peerfd剔除监听队列，被上传文件的客户端独占
                    epollDelEvent(pthreadpool->epfd,ptask->peerfd);
                    send(ptask->peerfd, "Confirm", sizeof("Confirm"), 0);
                    func_puts_filename(ptask);
                    netdiskLog("CMD:PUTS FILE exec one time.\n");
                    //独占结束后，将peerfd重新添加回epfd中。
                    epollAddReadEvent(pthreadpool->epfd,ptask->peerfd);
                    free(ptask);
                    break;  

            }
            case CMD_TYPE_LOGIN:
                {
                    puts("CMD_TYPE_LOGIN");
                    epollDelEvent(pthreadpool->epfd,ptask->peerfd);
                    printf("The CMD is %d\n", ptask->type);
                    printf("What received: ");
                    puts(ptask->data);
                    if(!func_password(ptask->peerfd, ptask->data)) epollAddReadEvent(pthreadpool->epfd,ptask->peerfd);
                    netdiskLog("CMD:LOGIN exec one time.\n");
                    free(ptask);
                    break;
                }
            default:
                break;
            }
        }
		 else {
			break;
		}
	}
	printf("sub thread %ld is ready to exit\n", pthread_self());
	pthread_exit(0);
}
